﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Net;

namespace UVMComm
{
    // IData를 인스턴스 할 수 없게 abstract Class로 구성한다.
    class IData
    {
        private byte[] m_data;
        private int m_datalength;
        public int Length
        {
            get { return m_datalength; }
        }
        protected void SetLength(int length)
        {
            m_datalength = length;
        }
        public byte[] DATA
        {
            get { return m_data; }
            set { m_data = value; }
        }
        public byte[] GetBytes()
        {
            return DATA;
        }
        public IData(byte[] bytes)
        {
            DATA = bytes;
            if(bytes != null)
                SetLength(bytes.Length);
        }
    };

    class CLA01DATA : IData
    {
        public CLA01DATA(object[] args) : base(null)
        {
            DATA = GetData(args[0], args[1]);
            SetLength(DATA.Length);
        }
        private byte[] GetData(object pwd, object date)
        {
            string retstr = (string)pwd + ((DateTime)date).ToString("yyMMdd");
            return md5util.getMd5HashBytes(retstr);
        }
    };

    class CLA02DATA : IData
    {
        public CLA02DATA(object[] args)
            : base(null)
        {
            DATA = GetData(args[0], args[1]);
            SetLength(DATA.Length);
        }
        private byte[] GetData(object ipaddrs, object result_id)
        {
            List<byte> bytelist = new List<byte>();
            bytelist.AddRange(GetAddressBytesFromList((List<string>)ipaddrs));
            bytelist.AddRange(GetResult_IdToBytes((int)result_id));
            return bytelist.ToArray();
        }
        private byte[] GetAddressBytesFromList(List<string> addrs)
        {
            List<byte> bytesList = new List<byte>();
            byte Seperator = (byte)';';
            foreach (string item in addrs)
            {
                bytesList.AddRange(GetAddressByteArray(item));
                bytesList.Add(Seperator);
            }
            return bytesList.ToArray();
        }
        private byte[] GetAddressByteArray(string addr)
        {
            IPAddress ip = IPAddress.Parse(addr);
            return ip.GetAddressBytes();
        }
        private byte[] GetResult_IdToBytes(int result_id)
        {
            List<byte> bytelist = new List<byte>();
            char[] arr = result_id.ToString().ToCharArray();

            foreach (char item in arr)
                bytelist.Add((byte)item);
            return bytelist.ToArray();
        }
    };
}